<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');

class Broker extends Admin_Controller
{
	public function __construct()
	{
		parent::__construct();

		$this->load->model('user_broker_model', 'broker_model');
	}

	/**
	 * 经纪人管理页面
	 */
	public function index()
	{
		//get获取查询参数
		$page = intval($this->input->get('page', true)); //页数
		$where['groupid'] = $this->input->get('groupid', TRUE);
		$where['uid'] = $this->input->get('uid', TRUE);
		$where['username'] = $this->input->get('username', TRUE);
		$where['email'] = $this->input->get('email', TRUE);
		$where['like'] = $this->input->get('like', TRUE);

		//分页
		$count = $this->broker_model->count_all_list($where); //查询总数
		$config['base_url'] = site_url('admin/user_broker_manage/index?'); //基本url
		$config['total_rows'] = $count; //总数据条数
		$config['per_page'] = 10; //每页显示多少条数据
		$this->load->library('pagination', $config);

		//根据条件查询需要显示的数据
		$brokers = array();
		if ($count)
		{
			$limit = $config['per_page']; //$this->pagination->per_page;
			$offset = $this->pagination->get_offset($page);
			$brokers = $this->broker_model->get_all_list($where, $limit, $offset, 'uid DESC');
		}

		//获取用户组信息
		$this->load->model('user_group_model', 'group_model');
		$groups = $this->group_model->get_all();

		$data['groups'] = $groups;
		$data['brokers'] = $brokers;
		$data['get'] = $where;
		$this->load->helper('form');
		$this->load->view('admin/broker_index', $data);
	}

	/**
	 * 经纪人编辑页面
	 */
	public function edit()
	{
		$uid = $this->input->get('uid', TRUE);
		!$uid && show_admin_msg('非法操作：UID不能为空');

		$broker = $this->broker_model->get($uid);
		!$broker && show_admin_msg('记录不存在或已删除');

		//获取用户组信息
		$this->load->model('user_group_model', 'group_model');
		$groups = $this->group_model->get_all();

		$data['groups'] = $groups;
		$data['broker'] = $broker;
		$data['uid'] = $uid;
		$this->load->helper('form');
		$this->load->view('admin/broker_edit', $data);
	}

	/**
	 * 经纪人更新操作
	 */
	public function do_edit()
	{
		//input
		$post = $this->input->post(NULL, TRUE);

		//验证
		$status = array(
			'unactive',
			'active',
			'lock');
		!in_array($post['status'], $status) && show_admin_msg('经纪人状态提交的数据非法');

		//数据处理
		$post['createtime'] = intval(strtotime($post['createtime']));
		$post['verify'] = implode(',', $post['verify']);

		//入库数据
		$update['uid'] = $post['uid']; //用户UID
		$update['groupid'] = $post['groupid']; //用户组
		$update['realname'] = $post['realname']; //真实姓名
		$update['company'] = $post['company']; //所属公司
		$update['address'] = $post['address']; //工作地址
		$update['verify'] = $post['verify']; //认证类型
		$update['idcard'] = $post['idcard']; //身份证号码
		//$update['idcardpic'] = $post['idcardpic']; //身份证图片
		$update['alipay'] = $post['alipay']; //支付宝帐号
		$update['phone'] = $post['phone']; //手机号码
		$update['createtime'] = $post['createtime']; //加入时间
		$update['remark'] = $post['remark']; //经纪人备注
		$update['announce'] = $post['announce']; //店铺公告
		$update['status'] = $post['status']; //状态

		$this->broker_model->update($update);

		redirect('admin/broker/edit?uid=' . $post['uid']);
	}

	/**
	 * 更改经纪人状态
	 */
	public function change_status()
	{
		$uid = $this->input->get('uid', TRUE);
		$status = $this->input->get('status', TRUE);

        !$uid && show_admin_msg('非法操作：UID不能为空');

		$broker = $this->broker_model->get($uid);
		!$broker && show_admin_msg('经纪人不存在或已删除');

		if ($status == 'lock')
		{
			$this->broker_model->lock($uid);
			show_admin_msg('锁定成功', 'success');
		}
		elseif ($status == 'active')
		{
			$this->broker_model->active($uid);
			show_admin_msg('激活成功', 'success');
		}
		elseif ($status == 'unlock')
		{
			$this->broker_model->unlock($uid);
			show_admin_msg('解锁成功', 'success');
		}

	}
}

/* End of file user_manage.php */
/* Location: ./application/controllers/admin/user_manage.php */
